<!--
 * @Description: 会员等级
 * @Author: DXV-RGWU-TUFH-RFCY-IEGMYY
 * @Date: 2023-08-08 16:06:24
 * @LastEditTime: 2023-08-10 16:02:32
 * @LastEditors: DXV-RGWU-TUFH-RFCY-IEGMYY
-->

<template>
  <div class="lb-custom-member-level-list">
    <top-nav></top-nav>
    <div class="page-main">
      <lb-tips
        >等级会员支持自动升级，升级判断条件根据成长值核算，会员等级到每年的1月1号重新核算</lb-tips
      >
      <el-row class="page-top-operate flex-warp">
        <lb-button
          type="primary"
          @click="$router.push(`/custom/member/level/edit`)"
          icon="el-icon-plus"
          v-hasPermi="`${$route.name}-add`"
          >{{ $t('menu.CustomMemberLevelAdd') }}</lb-button
        >
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="" label="会员等级" min-width="100" fixed>
          <template slot-scope="scope">VIP{{ scope.row.rankTop }}</template>
        </el-table-column>
        <el-table-column prop="title" label="等级名称" min-width="120">
        </el-table-column>
        <el-table-column prop="growth" label="获得等级条件" min-width="120">
          <template slot-scope="scope">
            成长值达到{{ scope.row.growth }}
          </template>
        </el-table-column>
        <el-table-column prop="rights" label="会员权益" min-width="180">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.rights" :key="index">
              <div v-if="index < 3">{{ index + 1 }}、{{ item.title }}；</div>
            </div>
            <div v-if="scope.row.rights.length > 3">......</div>
          </template>
        </el-table-column>
        <el-table-column prop="member_num" label="" min-width="120"
          ><template slot="header" slot-scope="scope">
            <div style="margin-top: 7px">
              会员数量<lb-tool-tips padding="10"
                >当前等级的会员数量</lb-tool-tips
              >
            </div>
          </template>
          <template slot-scope="scope">{{ scope.row.member_num }}</template>
        </el-table-column>
        <el-table-column prop="color" label="等级颜色" min-width="100">
          <template slot-scope="scope">
            <div
              class="radius-10"
              style="width: 50px; height: 30px"
              :style="{ background: scope.row.color }"
            ></div>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="等级颜色" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.status === 1 ? '使用中' : '已停用' }}
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间" min-width="120">
          <template slot-scope="scope">
            <p>{{ scope.row.create_time | handleTime(1) }}</p>
            <p>{{ scope.row.create_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="操作" min-width="160">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                type="primary"
                size="mini"
                plain
                @click="
                  $router.push(`/custom/member/level/edit?id=${scope.row.id}`)
                "
                v-hasPermi="`${$route.name}-edit`"
                >{{ $t('action.edit') }}</lb-button
              >
              <lb-button
                type="success"
                size="mini"
                plain
                @click="confirmDel(scope.row.id, 1, scope.row.rankTop)"
                v-hasPermi="`${$route.name}-start`"
                v-show="scope.row.status === 0"
                >{{ $t('action.start') }}</lb-button
              >
              <lb-button
                type="danger"
                size="mini"
                plain
                @click="confirmDel(scope.row.id, 0, scope.row.rankTop)"
                v-hasPermi="`${$route.name}-stop`"
                v-show="scope.row.status === 1"
                >{{ $t('action.stop') }}</lb-button
              >
              <!-- scope.row.status === 1 &&
                  (scope.row.rankTop === total ||
                    (scope.row.rankTop < total &&
                      tableData[scope.row.rankTop].status === 0)) -->
              <lb-button
                type="danger"
                size="mini"
                plain
                @click="confirmDel(scope.row.id, -1)"
                v-hasPermi="`${$route.name}-delete`"
                v-show="scope.row.status === 0"
                >{{ $t('action.delete') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
export default {
  data () {
    return {
      loading: false,
      tableData: [],
      searchForm: {
        page: 1,
        limit: 10
      },
      total: 0
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    getTableDataList () {
      this.loading = true
      let { searchForm } = this
      let { page, limit } = searchForm
      this.$api.member.levelList(searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          let { data, total } = res.data
          data.map((item, index) => {
            item.rankTop = index * 1 + ((page - 1) * limit + 1)
          })
          this.tableData = data
          this.total = total
        }
      })
    },
    confirmDel (id, status, top = 0) {
      let statusText = {
        '-1': '删除',
        0: '停用',
        1: '启用'
      }
      let top1 = top - 1
      let msgText = {
        '-1': `删除之后，在有效期内的该等级会员会自动降级到下一个正常状态的等级！`,
        0: `停用等级 VIP${top} 后，原来 VIP${top} 等级的用户在有效期内可正常使用，有效期之后重新核算!`,
        1: top === 1 ? `` : `启用等级 VIP${top} 后，原 VIP${top1} 的满级用户会发生变动，请谨慎操作!`
      }
      let msg = msgText[status]
      const h = this.$createElement
      this.$confirm(h('p', null, msg ? [
        h('p', null, `你确认要${statusText[status]}吗？`),
        h('p', { style: 'font-size:13px;line-height:1.2;color: red' }, msg)
      ] : [
        h('p', null, `你确认要${statusText[status]}吗？`)
      ]), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updateItem(id, status)
      }).catch(() => {

      })
    },
    async updateItem (id, status) {
      this.$api.member.levelStatusUpdate({ id, status }).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          if (status === -1) {
            this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
          }
          this.getTableDataList()
        }
      })
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.page-main {
  .el-input,
  .el-input-number,
  .el-select {
    width: 300px;
  }
  .el-textarea {
    width: 600px;
  }
  .edui-default {
    width: 95%;
  }
}
</style>
